<template>
  <div class="common-layout">
    <el-container class="app-container">
      <!-- 头部导航栏 -->
      <el-header class="header-container">
        <div class="header-left">
          <h1 class="system-title">爱吃兔溯源系统</h1>
        </div>
        <div class="header-right">
          <span class="welcome-message">欢迎用户 {{ currentUser.nickname || currentUser.username }} 登录</span>
          <el-button type="primary" class="logout-button" @click="handleLogout()" size="small">
            退出登录
          </el-button>
        </div>
      </el-header>

      <!-- 主体内容区 -->
      <el-container class="main-container">
        <!-- 侧边栏菜单 -->
        <el-aside width="200px" class="aside-container">
          <div class="menu-header">
            <h5 class="menu-title">爱吃兔(溯源)</h5>
          </div>
          <el-menu active-text-color="#ffd04b" background-color="#545c64" text-color="#fff" router
            default-active="/Dashboard" class="sidebar-menu">
            <el-sub-menu index="1">
              <template #title>
                <el-icon>
                  <location />
                </el-icon>
                <span>功能菜单</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="/Dashboard">首页</el-menu-item>
                <el-menu-item index="/Goods">商品管理</el-menu-item>
                <el-menu-item index="/Traceability">溯源查询</el-menu-item>
                <el-menu-item index="/Demo">让我测一测</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <!-- 主内容区域 -->
        <el-main class="content-container">
          <transition name="fade-in" mode="out-in">
            <RouterView />
          </transition>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useCounterStore } from '@/stores/counter';
import { Location } from '@element-plus/icons-vue';

const pinia = useCounterStore();
const router = useRouter();
const route = useRoute();

// 当前登录用户信息
const currentUser = ref({
  username: pinia.UserInfo.UserName,
  nickname: pinia.UserInfo.RealName,
});

// 默认激活的菜单
// const defaultActiveMenu = computed(() => {
//   // 根据当前路由设置默认激活的菜单
//   const path = route.path;
//   return path === '/Main' || path === '/' ? '/Goods' : path;
// });

// 处理退出登录
const handleLogout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '退出登录',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(() => {
      // 清除用户登录状态
      pinia.UserInfo.RealName = "";
      pinia.UserInfo.AccountToken = '';

      ElMessage.success('退出登录成功');
      // 跳转到登录页面
      router.push('/');
    })
    .catch(() => {
      ElMessage.info('已取消退出');
    });
};
</script>

<style scoped>
/* 全局容器样式 */
.common-layout {
  height: 100vh;
  overflow: hidden;
}

/* 应用容器 */
.app-container {
  height: 100%;
  overflow: hidden;
}

/* 主容器样式 */
.main-container {
  height: calc(100vh - 70px);
  overflow: hidden;
}

/* 主内容区域样式 */
.content-container {
  background-color: #f5f7fa;
  padding: 20px;
  overflow-y: auto;
  transition: all 0.3s ease;
  height: 100%;
}

/* 头部导航栏样式 */
.header-container {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 30px;
  height: 70px;
  z-index: 100;
  position: relative;
}

.header-left {
  display: flex;
  align-items: center;
}

.system-title {
  font-size: 24px;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  margin: 0;
  letter-spacing: 1px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 25px;
}

.welcome-message {
  color: #f0f0f0;
  font-size: 15px;
  background: rgba(255, 255, 255, 0.15);
  padding: 8px 16px;
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.logout-button {
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  font-weight: 500;
}

.logout-button:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

/* 侧边栏样式 */
.aside-container {
  background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
  border-right: none;
  transition: all 0.3s ease;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
}

.menu-header {
  padding: 20px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  text-align: center;
}

.menu-title {
  color: #ecf0f1;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  text-align: center;
  letter-spacing: 1px;
}

.sidebar-menu {
  height: calc(100% - 70px);
  border-right: none;
  background: transparent;
}

/* 菜单悬停效果增强 */
:deep(.el-menu-item:hover),
:deep(.el-sub-menu__title:hover) {
  background: rgba(255, 255, 255, 0.1) !important;
}

/* 当前激活的菜单样式 */
:deep(.el-menu-item.is-active) {
  background: linear-gradient(90deg, #3498db, #2c3e50) !important;
  color: white !important;
  border-left: 4px solid #f39c12;
}

/* 菜单项样式优化 */
:deep(.el-menu-item) {
  color: #ecf0f1;
  transition: all 0.3s ease;
}

:deep(.el-menu-item:hover) {
  color: #3498db !important;
}

:deep(.el-sub-menu__title) {
  color: #ecf0f1;
}

:deep(.el-sub-menu__title:hover) {
  color: #3498db !important;
}

/* 菜单图标样式 */
:deep(.el-icon) {
  color: #ecf0f1;
  margin-right: 8px;
}

:deep(.el-menu-item.is-active .el-icon) {
  color: white !important;
}

/* 主内容区域样式 */
.content-container {
  background-color: #f5f7fa;
  padding: 20px;
  overflow-y: auto;
  transition: all 0.3s ease;
}

/* 滚动条样式优化 */
.content-container::-webkit-scrollbar {
  width: 8px;
}

.content-container::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.content-container::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 4px;
}

.content-container::-webkit-scrollbar-thumb:hover {
  background: #555;
}

/* 过渡动画 */
.fade-in-enter-active,
.fade-in-leave-active {
  transition: opacity 0.3s ease;
}

.fade-in-enter-from,
.fade-in-leave-to {
  opacity: 0;
}

/* 响应式设计 */
@media screen and (max-width: 1024px) {
  .aside-container {
    width: 180px !important;
  }

  .system-title {
    font-size: 18px;
  }

  .welcome-message {
    display: none;
  }
}

@media screen and (max-width: 768px) {
  .header-container {
    padding: 0 15px;
  }

  .aside-container {
    width: 0 !important;
    overflow: hidden;
  }

  .content-container {
    padding: 15px;
  }
}
</style>
